package com.uchedao.seller.ui.carlist;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uchedao.seller.R;
import com.uchedao.seller.ui.carlist.entity.CarEntity;
import com.uchedao.seller.ui.carlist.fragment.FragmentBrand;
import com.uchedao.seller.ui.carlist.fragment.FragmentModel;
import com.uchedao.seller.ui.carlist.fragment.FragmentSeries;
import com.uchedao.seller.util.LogUtil;

/**
 * Created by ZhangZhanghua on 2015/9/14 0014.
 */
public class CarActivity extends BaseActivity implements FragmentBrand.IBrandClickListener, FragmentSeries.ISeriesClickListener, FragmentModel.IModelClickListener {
    public static final String EXTRA_DATA = "extra_data_carEntity";
    private FragmentBrand fBrand;
    private FragmentModel fModel;
    private FragmentSeries fSeries;
    private int bId, sId, mId;
    private String bName, sName, mName;
    private TextView tvBrand, tvSeries, tv1;
    private FragmentManager fm;
    private FragmentTransaction ft;
    private LinearLayout ll;
    private Intent intent;
    private CarEntity carEntity;
    private Bundle bundle;

    @Override
    protected String getActivityTag() {
        return "CarActivity";
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_car;
    }

    @Override
    protected void initView() {
        TextView tv = (TextView) findViewById(R.id.tv_title_name);
        tv.setText("车辆选择");
        carEntity = new CarEntity();
        ll = (LinearLayout) findViewById(R.id.ll_container);
        tvBrand = (TextView) findViewById(R.id.tv_brand);
        tvSeries = (TextView) findViewById(R.id.tv_series);
        tv1 = (TextView) findViewById(R.id.tv_1);
        ll.setVisibility(View.GONE);
        setTvGone();
        fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        if (getIntent().getExtras() != null) {
            switch (getIntent().getExtras().getInt("carSelect")) {
                case 1:
                    fSeries = new FragmentSeries();
                    ft.add(R.id.content, fSeries);
                    ft.commit();
                    break;
                case 2:
                    fModel = new FragmentModel();
                    ft.add(R.id.content, fModel);
                    ft.commit();
                    break;
            }

        } else {
            fBrand = new FragmentBrand();
            ft.add(R.id.content, fBrand);
            ft.commit();
        }

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

        findViewById(R.id.title_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvBrand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ft = fm.beginTransaction();
                ft.replace(R.id.content, fBrand);
                ft.commit();
                setTvGone();
                ll.setVisibility(View.GONE);
            }
        });
        tvSeries.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle args = new Bundle();
                args.putInt("carBrandId", bId);
                fSeries.setArguments(args);
                ft = fm.beginTransaction();
                ft.replace(R.id.content, fSeries);
                setTvGone();
                setTvVisible(tvBrand);
                setTvVisible(tv1);
                ft.commit();
            }
        });
    }

    @Override
    public void onBrandClick(int bId, String bName) {
        ll.setVisibility(View.VISIBLE);
        this.bId = bId;
        this.bName = bName;
        Bundle args = new Bundle();
        args.putInt("carBrandId", this.bId);
        setTvGone();
        setTvVisible(tvBrand);
        tvBrand.setText(bName);
        if (fSeries == null) {
            fSeries = new FragmentSeries();
        }
        fSeries.setArguments(args);
        ft = fm.beginTransaction();
        ft.replace(R.id.content, fSeries);
        ft.commit();
        carEntity.setbId(this.bId);
        carEntity.setbName(this.bName);
        intent = new Intent();
        bundle = new Bundle();
        bundle.putSerializable(EXTRA_DATA, carEntity);
        intent.putExtras(bundle);
        setResult(1, intent);
    }

    @Override
    public void onSeriesClick(int sId, String sName) {
        this.sId = sId;
        this.sName = sName;
        Bundle args = new Bundle();
        args.putInt("carSeriesId", this.sId);
        setTvGone();
        setTvVisible(tvBrand);
        tvBrand.setText(bName);
        setTvVisible(tv1);
        setTvVisible(tvSeries);
        tvSeries.setText(sName);
        if (fModel == null) {
            fModel = new FragmentModel();
        }
        fModel.setArguments(args);
        ft = fm.beginTransaction();
        ft.replace(R.id.content, fModel);
        ft.commit();
        carEntity.setsId(this.sId);
        carEntity.setsName(this.sName);
        intent = new Intent();
        bundle = new Bundle();
        bundle.putSerializable(EXTRA_DATA, carEntity);
        intent.putExtras(bundle);
        setResult(2, intent);
    }

    public void setTvGone() {
        tvBrand.setVisibility(View.GONE);
        tvSeries.setVisibility(View.GONE);
        tv1.setVisibility(View.GONE);
    }

    public void setTvVisible(TextView tv) {
        tv.setVisibility(View.VISIBLE);
    }

    @Override
    public void onModelClick(int mId, String mName) {
        if (mId != 0) {
            this.mId = mId;
        }
        if (!mName.equals("0")) {
            this.mName = mName;
        } else {
            this.mName = "";
        }
        carEntity.setmId(this.mId);
        carEntity.setmName(this.mName);
        intent = new Intent();
        bundle = new Bundle();
        bundle.putSerializable(EXTRA_DATA, carEntity);
        intent.putExtras(bundle);
        setResult(RESULT_OK, intent);
        LogUtil.e("bId:" + bId + "sId:" + sId + "mId:" + this.mId + "bName:" + bName + "sName:" + sName + "mName:" + this.mName);
    }

}